﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using YAF.Classes;
using YAF.Classes.Data;
using YAF.Core;
using YAF.Types.Constants;
using YAF.Types.Interfaces;

namespace YAF.pages.admin
{
    public partial class hotnewsmanager : AdminPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            // Latest forum posts
            // Shows the latest n number of posts on the main forum list page
            const string CacheKey = Constants.Cache.ForumActiveDiscussions;

            DataTable activeTopics = null;

            if (this.PageContext.IsGuest)
            {
                // allow caching since this is a guest...
                activeTopics = this.Get<IDataCache>()[CacheKey] as DataTable;
            }

            if (activeTopics == null)
            {
                this.Get<IYafSession>().UnreadTopics = 0;

                activeTopics = LegacyDb.topic_latest(
                    boardID: this.PageContext.PageBoardID,
                    numOfPostsToRetrieve: this.Get<YafBoardSettings>().ActiveDiscussionsCount,
                    pageUserId: this.PageContext.PageUserID,
                    useStyledNicks: this.Get<YafBoardSettings>().UseStyledNicks,
                    showNoCountPosts: this.Get<YafBoardSettings>().NoCountForumsInActiveDiscussions,
                    findLastRead: this.Get<YafBoardSettings>().UseReadTrackingByDatabase);

                // Set colorOnly parameter to true, as we get all but color from css in the place
                if (this.Get<YafBoardSettings>().UseStyledNicks)
                {
                    this.Get<IStyleTransform>().DecodeStyleByTable(ref activeTopics, true, "LastUserStyle");
                }

                if (this.PageContext.IsGuest)
                {
                    this.Get<IDataCache>().Set(
                        CacheKey,
                        activeTopics,
                        TimeSpan.FromMinutes(this.Get<YafBoardSettings>().ActiveDiscussionsCacheTimeout));
                }
            }
            this.grdToppic.DataSource = activeTopics;
            this.grdToppic.DataBind();

        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            int s ;
            for (int i = 0; i < grdToppic.Rows.Count; i++)
            {
                GridViewRow row = grdToppic.Rows[i];
                bool isChecked = ((CheckBox)row.Cells[0].FindControl("cbTopicID")).Checked;
                if (isChecked)
                {
                    s=Convert.ToInt32(grdToppic.Rows[i].Cells[1].Text);
                    try
                    {
                        LegacyDb.topic_hot_add(s);
                        message.Text = "cập nhật thành công";
                    }
                    catch (Exception)
                    {

                        message.Text = "cập nhật không thành công";
                }
            }
           // txtID.Text = s;
           // s = s.Substring(0,s.Length - 1);
              
            }
            
        }
    }
}